<!--
 * @Author: White
 * @description: 歌曲详细信息
 * @LastEditTime: 2021-09-01 10:40:40
-->

<template>
    <div class="music-main" v-if="songList">
        <div class="music-cover">
            <img :src="songList.songs[0].al.picUrl" :class="player? 'cover':''">
        </div>
        <div class="music-info">
            <h3 class="music-title">{{songList.songs[0].name}}</h3>
            <div class="music-desc">{{songList.songs[0].ar[0].name}}{{songList.songs[0].alia[0]}}</div>

            <!-- 歌词 -->
            <div class="music-words">
                <v-html class="music-html" style="white-space: pre-wrap">{{list}}</v-html>
            </div>
        </div>
    </div>
    <!-- 评论信息 -->
    <div class="music-comment">
        <!-- 发送评论 -->
        <div class="music-discuss">
            <div class="discuss-title">
                <h3>听友评论</h3>
                <p>共有条{{total}}评论</p>
            </div>
            <div class="discuss-post">
                <input type="text" @click="popover">
            </div>
        </div>
        <!-- 精彩评论 -->
        <div class="music-list">
            <div class="music-list-item" v-for="(item, index) in discuss" :key="index">
                <div class="item-image">
                    <img :src="item.user.avatarUrl">
                </div>
                <div class="item-text">
                    <div>
                        <span class="user">{{item.user.nickname}}:</span>
                        <span class="text">{{item.content}}</span>
                    </div>
                    <div class="item-time">
                        <div class="time">{{timeFormat(item.time)}}</div>
                        <div class="goods">{{item.likedCount}}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div v-if="songList">
        <el-dialog
            :title="songList.songs[0].name"
            v-model="dialogVisible"
            width="30%">
            <input type="text" v-model="input" placeholder="发表评论">
            <template #footer>
                <span class="dialog-footer">
                <el-button type="primary" @click="handleSure">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { useStore } from 'vuex'
import { ref, computed, watch, toRefs, reactive } from 'vue'
import { getLyric, getDiscuss, postDiscuss } from '@/api/music'
import { timeFormat } from '@/utils/index'
export default {
    setup () {
        const store = useStore()
        const dialogVisible = ref(false);
        const songList = computed(() => {
            return store.state.currentSong
        })
        const lyric = reactive({
            list: '',
            input: '',
            discuss: '',
            total: ''
        })
        // 当前播放歌曲信息
        const player = computed(() => {
            return store.state.player
        })
        // 弹窗
        function popover() {
            dialogVisible.value = true
        }
        const handleSure = () => {
            dialogVisible.value = false
            Postcomment()
        }

        // 歌曲变化刷新歌词
        watch(() => songList.value, () => {
            getGeci()
        })

        async function getGeci() {
            const parma = { id: songList.value.privileges[0].id }
            const res = await getLyric(parma)
            var pattern = new RegExp(/([0-9])/g) 
            lyric.list = res.data.lrc.lyric.replace(pattern, '').replace(/:/g, '').replace(/\./g,'').replace(/\[/g,'').replace(/\]/g,'')

            const discuss = await getDiscuss(parma)
            lyric.discuss = discuss.data.hotComments
            lyric.total = discuss.data.total
        }

        async function Postcomment() {
            const parma = {
                id: songList.value.privileges[0].id,
                t: 1,
                type: 1,
                content: lyric.input
            }
            const res = await postDiscuss(parma)
            console.log(res)
        }

        return {
            ...toRefs(lyric),
            dialogVisible,
            handleSure,
            songList,
            popover,
            timeFormat,
            player
        }
    }
}
</script>

<style lang="scss" scoped>
input {
    width: 100%;
    height: 90px;
    box-sizing: border-box;
}

@keyframes turn{
    0%{-webkit-transform:rotate(0deg);}
    25%{-webkit-transform:rotate(90deg);}
    50%{-webkit-transform:rotate(180deg);}
    75%{-webkit-transform:rotate(270deg);}
    100%{-webkit-transform:rotate(360deg);}
}
.cover {
    animation:turn 15s linear infinite;
}
.music-main {
    display: flex;
    height: 500px;
    justify-content: space-between;
    box-sizing: border-box;
    
    .music-cover {
        width: 45%;
        height: 500px;
        display: flex;
        align-items: center;
        img {
            width: 300px;
            height: 300px;
            border-radius: 50%;
        }
    }
    .music-info::-webkit-scrollbar {
        display: none;
    }
    .music-info {
        width: 55%;
        height: 500px;
        overflow-y: scroll;
        .music-title {
            font-size: 24px;
            font-weight: 400;
            line-height: 40px;
        }
        .one {
            color: red;
        }
        .music-desc {
            line-height: 20px;
            margin-top: 20px;
        }
        .music-tag {
            line-height: 20px;
        }
        .music-words {
            font-size: 14px;
            margin-top: 20px;
            .music-html {
                line-height: 30px !important;
                color: gray;
            }
        }
    }
}
// 评论
.music-comment {
    margin-top: 40px;
    .music-discuss {
        margin-bottom: 20px;
        .discuss-title {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            h3 {
                font-size: 20px;
                margin-right: 30px;
            }
            p {
                font-size: 14px;
                color: gray;
            }
        }
        .discuss-post {
            input {
                width: 100%;
                height: 50px;
                border-radius: 10px;
                border: 1px solid gray;
                padding-left: 10px;
                outline:none;
            }
        }
    }
    .music-list {
        .music-list-item {
            display: flex;
            padding: 10px 0px;
            .item-image {
                margin-right: 10px;
                img {
                    width: 30px;
                    height: 30px;
                    border-radius: 50%;
                }
            }
            .item-text {
                font-size: 14px;
                width: 100%;
                .user {
                    margin-right: 10px;
                    color: #5e7fac;
                }
                .text {
                    line-height: 20px;
                }
                .item-time {
                    margin-top: 10px;
                    color: gray;
                    font-size: 10px;
                    display: flex;
                    justify-content: space-between;
                }
            }
        }
    }
}
</style>
